package cn.itsource.product.service.impl;

import cn.itsource.basic.config.BussinessException;
import cn.itsource.basic.util.CodeGenerateUtils;
import cn.itsource.order.domain.OrderProduct;
import cn.itsource.order.service.IOrderProductService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.pet.dto.AcceptDto;
import cn.itsource.product.domain.ProductTohandle;
import cn.itsource.product.mapper.ProductTohandleMapper;
import cn.itsource.product.service.IProductTohandleService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-11-03
 */
@Service
public class ProductTohandleServiceImpl extends BaseServiceImpl<ProductTohandle> implements IProductTohandleService {
    @Autowired
    private ProductTohandleMapper productTohandleMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IOrderProductService orderProductService;
    /**
     * 接单接口
     * @param acceptDto
     */
    @Override
    public void accept(AcceptDto acceptDto) {
        // 1. 根据msgId查询虚竹消息
        ProductTohandle productTohandle = productTohandleMapper.findById(acceptDto.getMsgId());
        // 2.非空校验
        if (productTohandle == null ){
            throw new BussinessException("消息不能为空");
        }
        // 3.上锁解决线程高并发问题，防止多个员工抢单
        synchronized (productTohandle.getId()) {
            // 查询接单的员工信息，并获取他所在的店铺信息
            Employee employee = employeeService.findById(acceptDto.getHandler());
            // 校验
            if (employee == null) {
                throw new BussinessException("不存在此员工！");
            }
            initOrder(acceptDto, productTohandle, employee);
            
        }
    }


    /**
     * 拒单
     * @param msgId
     * @param employee
     */
    @Override
    public void reject(Long msgId, Employee employee) {
        // 校验
        ProductTohandle productTohandle = productTohandleMapper.findById(msgId);
        if (productTohandle == null) {
            throw new BussinessException("寻主消息不能为空");
        }
        // 添加拒单日志
        // initRejectLog(msgId, employee);
    }

    /**
     * 添加拒单日志
     * @param msgId
     * @param employee
     */



    /**
     * 初始化收购订单
     * @param acceptDto
     * @param productTohandle
     * @param employee
     */
    private void initOrder(AcceptDto acceptDto, ProductTohandle productTohandle, Employee employee) {
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setOrderSn(CodeGenerateUtils.generateOrderSn(acceptDto.getHandler()));
        orderProduct.setDigest(acceptDto.getNote());
        orderProduct.setState(0);
        orderProduct.setPrice(new BigDecimal(String.valueOf(productTohandle.getPrice())));
        orderProduct.setUserId(productTohandle.getUserId());
        orderProduct.setShopId(employee.getShopId());
        orderProductService.add(orderProduct);
    }
}
